package arm_search_3;
import arm_search_3.data.Seed;
import arm_search_3.data.email.EmailSeed;
import arm_search_3.process.email.EmailIndexGenerator;
import arm_search_3.query.email.EmailQueryProcessor;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Muqtafi Akhmad
 */
public class ARM_Search_3 {

    public static void main(String[] args) {
        if ((args[0].equals("build") && args.length != 4) || (args[0].equals("query") && args.length != 5)) {
            System.out.println("Invalid argument count");
            System.out.print("## Arguments for retrieval should be : query <index_directory> <query_file> <is_compressed : 1 / 0> <n_top>");
            System.out.print("## Arguments for build index should be : build <email_directory> <worker_number> <is_conmpressed : 1 / 0>");
            System.exit(1);
        } else {
            boolean isCompressed = false;
            if (args[3].equals("1")) {
                isCompressed = true;
            }
            if (args[0].equals("build")) {
                // build index
                String databaseDirectory = args[1];
                Integer threadCount = Integer.parseInt(args[2]);
                Seed rootSeed = new EmailSeed(databaseDirectory);
                ArrayList<String> seedFilter = new ArrayList<>();
                seedFilter.add("all_documents");
                EmailIndexGenerator indexGenerator = new EmailIndexGenerator(threadCount);
                System.out.println("## Reading statistics for directory " + databaseDirectory);
                long startTime = System.currentTimeMillis(), endTime;
                indexGenerator.doGenerateIndex(rootSeed, seedFilter, isCompressed);
                endTime = System.currentTimeMillis();
                System.out.println("## Statistics reading finished , elapsed time " + ((endTime - startTime) / 1000) + " seconds");
                System.exit(0);
            } else if (args[0].equals("query")) {
                try {
                    String indexDir = args[1];
                    String queryFile = args[2];
                    Integer n_top = Integer.parseInt(args[4]);
                    EmailQueryProcessor processor = new EmailQueryProcessor();
                    processor.initQueryProcessor(indexDir, isCompressed);
                    System.out.println("## Init query processor ");
                    Scanner in = new Scanner(new File(queryFile));
                    // membaca line per line, proses tiap line
                    while(in.hasNextLine()){
                        String query = in.nextLine();
                        // proses query
                        System.out.println("## Processing query : "+query);
                        processor.executeQuery(indexDir, query, isCompressed, n_top);
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ARM_Search_3.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                System.out.println("## Unknown first parameter : " + args[0]);
            }
        }
    }
}
